package com.example.statistics.ui.bank

import android.content.Intent
import android.os.Bundle
import com.example.statistics.bean.BankFlowBean
import com.example.statistics.bean.DefineInputBean
import com.example.statistics.bean.StatementBean
import com.example.statistics.common.BaseInputActivity
import com.example.statistics.ex.goTimeFormat
import com.example.statistics.ex.maxLimit
import com.example.statistics.ex.orEmpty
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.DefaultConfig
import com.example.statistics.weight.TextProcessingView
import org.jetbrains.anko.toast
import kotlin.math.abs

/**
 * 对账单
 */
open class StatementAddActivity : BaseInputActivity<StatementBean>() {

    private var bankStatementBean: BankFlowBean? = null
    private var type = ""
    private var function = ""
    override val title: String
        get() = "对账单"
    override val url: String
        get() = UrlConfig.BANK_STATEMENT_ADD

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initParams()
    }

    open fun initParams() {
        val functionId = intent.getIntExtra(Constants.FUNCTION_ID, 0)
        if (function.isBlank() || functionId == 0) {
            toast("没有对应的账单")
            finish()
            return
        }
        type = when (function) {
            DefaultConfig.FUNC_SALES,
            DefaultConfig.FUNC_OTHER_INCOME,
            DefaultConfig.FUNC_PORT_SALES ->
                "收入"
            DefaultConfig.FUNC_GENERAL_EXPENSE,
            DefaultConfig.FUNC_PORT_PURCHASE ->
                "支出"
            else -> {
                throw IllegalArgumentException("参数异常")
            }
        }

        extentParams.add("function" to function)
        extentParams.add("functionId" to functionId)
    }

    override val inputDataList: List<DefineInputBean>
        get() {
            function = intent.getStringExtra(Constants.FUNCTION).orEmpty()
            val money = intent.getDoubleExtra(Constants.AMOUNT, 0.0)
            val list = mutableListOf(
                DefineInputBean("银行流水：", "_account", TextProcessingView.STYLE_SELECTOR_JUMPS, isNull = false,
                    jumps = Pair({
                        val intent = Intent(this, BankFlowListActivity::class.java)
                        intent.putExtra(Constants.ACTION, Constants.SELECTOR)
                        intent.putExtra(Constants.TYPE, type)
                        return@Pair intent
                    }) { serializable, list ->
                        bankStatementBean = serializable as BankFlowBean?
                        (bankStatementBean)?.let {
                            val date = it.date.goTimeFormat() + " " + it.time.substring(0, 5)
                            list["_account"]?.contentText = it.account.toString()
                            list["_money"]?.contentText = it.balance.toString()
                            list["date"]?.contentText = date
                            list["type"]?.contentText = it.type
                            extentParams.add("accountId" to it.accountId)
                            extentParams.add("bankFlowId" to it.ID)
                        }
                    }),
                DefineInputBean("流水时间：", "date", TextProcessingView.STYLE_DISPLAY),
                DefineInputBean("类        型：", "type", TextProcessingView.STYLE_DISPLAY),
                DefineInputBean("账户余额：", "_money", TextProcessingView.STYLE_DISPLAY),
                DefineInputBean("结算金额：", "money", TextProcessingView.STYLE_INPUT_FLOAT,
                    if (money == 0.0) "" else money.toString(), isNull = false,
                    afterTextChangeListener = { str, list ->
                        if (!list["_account"]?.contentText.isNullOrBlank()) {
                            str?.maxLimit(this@StatementAddActivity, abs(bankStatementBean?.balance.orEmpty().toFloat()))
                        }
                    }),
                DefineInputBean("备        注：", "remark", TextProcessingView.STYLE_INPUT_FLOAT),
            )

            if (function == "采购") {
                list.add(4, DefineInputBean("属        性：", "attribute", TextProcessingView.STYLE_SELECTOR,
                    singleDialog = Pair(listOf("定金", "保证金", "尾款", "税款", "尾税", "出清")) { _, _ -> }
                ))
            }
            return list
        }

}